<?php

class m_menus extends CI_Model{
	
	function _list($data){
		extract($data);
		$sql = "SELECT 
					menus_label,
					menus_kode,
					menus_double,
					menus_icon,
					menus_color,
					menus_url,
					menus_id
				FROM s_menus
				WHERE 
					menus_sys_active = '1'
		";
		
		if (@$menus_label != "") $sql .= " AND menus_label like '%".@$menus_label."%'";
		if (@$menus_kode != "") $sql .= " AND menus_kode like '%".@$menus_kode."%'";
		if (@$menus_double != "") $sql .= " AND menus_double like '%".@$menus_double."%'";
		if (@$menus_icon != "") $sql .= " AND menus_icon like '%".@$menus_icon."%'";
		if (@$menus_color != "") $sql .= " AND menus_color like '%".@$menus_color."%'";
		if (@$menus_url != "") $sql .= " AND menus_url like '%".@$menus_url."%'";
		
		$query = $this->db->query($sql);
		$records = $query->num_rows();
		
		$sql = "SELECT * FROM (".$sql.") k LIMIT ".(($page - 1) * $rows).",".$rows;
		$query = $this->db->query($sql);
		$result = $query->result();
		
		return '{ 
		  "total": "'.ceil($records / $rows).'", 
		  "page": "'.$page.'", 
		  "records": "'.$records .'",
		  "rows" : '.json_encode($result).'
		}';
	}
	
	function _insert($data){
		$this->db->insert('s_menus',$data);
		return 1;
	}
	
	function _update($data,$id){
		$this->db->where('menus_id',$id);
		$this->db->update('s_menus',$data);
		return 1;
	}
	
	function _delete($data,$id){
		$this->db->where('menus_id',$id);
		$this->db->update('s_menus',$data);
		return 1;
	}
}